package edu.uts.reekie.designer.implementation.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.Panel;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.*;

import edu.uts.reekie.designer.figures.*;

public class ContainerComponentFigure extends ViewComponentFigure {

	private static final int GUTTER_PANEL_WIDTH = 25;
	private static final int NAME_PANEL_HEIGHT = 0;
	
	private Panel mainPanel;
	private Panel gutterPanel;
	Figure multiple;
	Figure main;
	
	public ContainerComponentFigure() {
		
		//
		// set this figure details
		//
		setBorder(null);
		setLayoutManager(new FreeformLayout());
		
		multiple = new Figure();
		multiple.setBorder(new LineBorder(ColorConstants.black));
		
		multiple.setLayoutManager(new StackLayout());
		// set up other figure properties
		multiple.setBackgroundColor(ColorConstants.white);
		multiple.setOpaque(true);
		multiple.setVisible(false);
		multiple.setLocation(new Point(5,5+NAME_PANEL_HEIGHT));
		add(multiple);
		

		// set up the layout manager
		
		Dimension mainSize =DEFAULT_SIZE.shrink(0, NAME_PANEL_HEIGHT); 
		
		main = new Figure();
		main.setBorder(null);
		
		// set up other figure properties
		main.setSize(mainSize);
		main.setLocation(new Point(0,NAME_PANEL_HEIGHT));
		main.setVisible(true);
		// set up the main panel
		main.add(nameLabel);
		main.setBackgroundColor(ColorConstants.listBackground);
		main.setOpaque(true);
		add(main);
		
		
	
		// set up the layout manager
		FlowLayout layout = new FlowLayout(true);
		layout.setMajorSpacing(0);
		layout.setMinorSpacing(0);
		main.setLayoutManager(layout);
		
		//
		// set up the gutter panel
		//
		gutterPanel = new Panel();
		
		gutterPanel.setVisible(true);
		gutterPanel.setBackgroundColor(Colors.infrastructureBackground);
		gutterPanel.setBorder(new LineBorder(ColorConstants.black));
		gutterPanel.setSize(GUTTER_PANEL_WIDTH, mainSize.height);
		
		FixedSizeFlowLayout gutterLayout = new FixedSizeFlowLayout();
		gutterLayout.setHorizontal(false);
		gutterLayout.setMajorAlignment(FlowLayout.ALIGN_RIGHTBOTTOM);
		gutterLayout.setMinorAlignment(FlowLayout.ALIGN_CENTER);
		gutterPanel.setLayoutManager(gutterLayout);
		
		main.add(gutterPanel);
		
		//
		// set up the figure contents
		//
		
		mainPanel = new Panel();
		mainPanel.setVisible(true);
		mainPanel.setBorder(new LineBorder(ColorConstants.black));
		mainPanel.setBackgroundColor(ColorConstants.listBackground);
		mainPanel.setLayoutManager(new FixedSizeFlowLayout());
		
		mainPanel.setSize(mainSize.width-GUTTER_PANEL_WIDTH, mainSize.height);
		main.add(mainPanel);
		
		// set up the contents panel
		nameLabel = new Label("some label");
		nameLabel.setLocation(new Point(40,40));
		mainPanel.add(nameLabel);
		
		
	}
	
	
	
	/*
	public Rectangle getHandleBounds() {
		return main.getBounds();
	}
	*/
	
	public void setBounds(Rectangle rect){
		
		super.setBounds(rect);
		
		// adjust the inside panels
		final int horizontalInset = 0; 
		
		Dimension dMain = rect.getSize();
		
		
		if (isMultiple()){
			dMain = dMain.shrink(5, 5);
	
		}
		
		main.setSize(dMain);
		multiple.setSize(dMain);
		
		mainPanel.setSize(dMain.width-GUTTER_PANEL_WIDTH-horizontalInset,dMain.height-NAME_PANEL_HEIGHT);
		gutterPanel.setSize(GUTTER_PANEL_WIDTH,dMain.height-NAME_PANEL_HEIGHT);
		
	}
	
	public void setMultiple(boolean value){
		
		if (value==true){
			multiple.setVisible(true);
		} else {
			multiple.setVisible(false);
		}
		super.setMultiple(value);
	}
	

}
